// pages/corporate-certification-detail/corporate-certification-detail.js
import { Company } from '../../../models/company'
import {check} from '../../../util/identityCheck'

const companyModel = new Company()
import UploadImg from '../../../util/uploadImg'
// import UploadImgs from '../../util/uploadImgs'
const uploadImg = new UploadImg()
// const uploadImgs = new UploadImgs()
Page({
  data: {
    auditState: '',
    imgs: [],
    companyInfo: {
      companyName: '',
      companyDesc: '',
      contact: '',
      phone: '',
      licensePic: './images/example.png',
      infoPic: []
    }
  },

  clearImg (event) {
    let index = event.target.dataset.index
    let imgs = this.data.companyInfo.infoPic
    imgs.splice(index, 1)
    this.setData({
      'companyInfo.infoPic': imgs
    })
  },

  onLoad (options) {
    if(!check())return
  },

  onReady () {
    this.getCompanyInfo()
  },

  getCompanyInfo () {
    companyModel.info(res => {
      let auditState = res.auditState
      this.setData({
        auditState: auditState
      })
      if (res.infoPic) {
        res.infoPic = res.infoPic.split(',')
      }
      this.setData({
        'companyInfo': res
      })
    })
  },

  chooseLicenseImg () {
    uploadImg.chooseImg(res => {
      this.setData({
        'companyInfo.licensePic': res
      })
    })
  },

  chooseInfoImg () {
    uploadImg.chooseImg(imgUrl => {
      let imgList = this.data.companyInfo.infoPic
      let length = imgList.length
      imgList[length] = imgUrl
      this.setData({
        'companyInfo.infoPic': imgList
      })
    })
  },

  inputChange (event) {
    let key = `companyInfo.${event.currentTarget.dataset.type}`
    this.setData({
      [key]: event.detail
    })
  },

  previewImg (event) {
    let urls = event.currentTarget.dataset.urls
    wx.previewImage({
      urls: urls
    })
  },

  publish (event) {
    let companyInfo = this.data.companyInfo
    if (!companyInfo.companyName) {
      wx.showToast({
        title: '请输入公司名称',
        icon: 'none'
      })
      return
    }
    if (!companyInfo.companyDesc) {
      wx.showToast({
        title: '请输入公司简介',
        icon: 'none'
      })
      return
    }
    if (!companyInfo.contact) {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none'
      })
      return
    }
    if (!companyInfo.phone) {
      wx.showToast({
        title: '请输入联系人电话',
        icon: 'none'
      })
      return
    }
    if (companyInfo.licensePic == './images/example.png') {
      wx.showToast({
        title: '请上传营业执照',
        icon: 'none'
      })
      return
    }
    let imgs = this.data.imgs
    if (imgs[0].indexOf('http') >= 0) {
      companyInfo.infoPic = String(imgs)

      let auditState = this.data.auditState
      if (auditState) {
        companyModel.registerAgain(companyInfo,
          {
            userId: wx.getStorageSync('userInfo').user_id || 0
          },   res => {
            wx.navigateBack({
              delta: 2
            })
          })
      } else  {
        companyModel.register(companyInfo,
          {
            userId: wx.getStorageSync('userInfo').user_id || 0
          },res => {
            wx.navigateBack({
              delta: 1
            })
          })
      }
    } else {
      uploadImgs.upLoadImg(imgs, res => {
        this.setData({
          'imgs': res
        })
        companyInfo.infoPic = String(this.data.imgs)
        let auditState = this.data.auditState
        if (auditState) {
          companyModel.registerAgain(companyInfo,
            {
              userId: wx.getStorageSync('userInfo').user_id || 0
            },   res => {
              wx.navigateBack({
                delta: 2
              })
            })
        } else  {
          companyModel.register(companyInfo,
            {
              userId: wx.getStorageSync('userInfo').user_id || 0
            },res => {
              wx.navigateBack({
                delta: 1
              })
            })
        }
      })
    }
  },

  chooseImgs () {
    uploadImgs.chooseImg(5 , res => {
      let imgs = this.data.imgs
      res.forEach(url => {
        imgs.push(url)
      })
      this.setData({
        imgs
      })
    })
  },

  x (event) {
    let index = event.currentTarget.dataset.index
    let imgs = this.data.imgs
    imgs.splice(index, 1)
    this.setData({
      imgs
    })
  },

})
